/**
 * Created by admin on 2018/2/27.
 */
    //调用小节测试sectionTest.css
(function loadCss( ){
    var url=basePathOnline+'/home/css/preSchoolTest/sectionTest/sectionTest.css';
    var link = document.createElement( "link" );
    link.type = "text/css";
    link.rel = "stylesheet";
    link.href =  url;
    $(document.getElementsByTagName( "head" )[0]).append(link);
})();
var isSuccess=false;
var resultId="";
var setctionTestTemp='' +
        '<div class="sectionTest_header clearfix">' +
        '<div class="sectionTest_name">小节测试</div>' +
        '<div class="sectionTest_count"><span class="curCount">1/</span><span class="total">{{items.length}}</span>题</div>' +
        '</div>' +
        '<div class="sectionTest_body">' +
        '<div class="timuAndAnswer">' +
        '<ul class="timu"></ul>' +
        '</div>' +
        '</div>' +
        '<div class="sectionTest_footer">' +
        '<div class="btn lastQuestion">上一题</div>' +
        '{{if items.length==1}}' +
        '<div class="btn sectionTest_submit notAvailable">提交</div>' +
        '{{else}}' +
        '<div class="btn nextQuestion notAvailable">下一题</div>' +
        '<div class="btn sectionTest_submit">提交</div>' +
        '{{/if}}' +
        '</div>',
    sectionTestResultTemp=
        '<div class="sectionTest_header clearfix">' +
        '<div class="sectionTest_name">共{{items.totalQuestionCount}}道题，你答对了{{items.rightQuestionCount}}道题，正确率：' +
        '<span class="errorAns">{{items.accuracy}}%</span>' +
        '</div>' +
        '<div class="sectionTest_count">' +
        '{{if !isShowFooter}}' +
        '<i class="iconfont icon-guanbi"></i>' +
        '{{/if}}' +
        '</div>' +
        '</div>' +
        '<div class="sectionTestResult_body">' +
        '<div class="studySuggest">' +
        '<div class="sectionTestResult_body_title">学习建议：</div>' +
        '<div class="studySuggest_content">{{items.advise}}</div>' +
        '</div>' +
        '<div class="timuAndAnswer">' +
        '<div class="sectionTestResult_body_title">题目答案：</div>' +
        '<ul class="timu sectionTestResult"></ul>' +
        '</div>' +
        '</div>' +
        '{{if isShowFooter}}' +
        '<div class="sectionTest_footer">' +
        '<button class="sessBtn reTest" style="display:inline-block">重新测试</button>' +
        '<button class="sessBtn continue" style="display:inline-block">继续学习</button>' +
        '</div>' +
        '{{/if}}';

//小节测试
//sectionTest(872,8,21)
/**
 *
 * @param courseId 课程ID
 * @param moduleId  模块ID
 * @param sectionId 小节ID
 */
function  sectionTest(courseId,moduleId,sectionId){
    RequestService("/sectionTest/findSectionTestQuestion", "GET", {moduleId:moduleId,sectionId:sectionId}, function (data) {
        var paper=data.result;
        $(".scetionTest_lists").html(template.compile(setctionTestTemp)({
            items:paper
        }));
        $(".scetionTest_lists .timu").html(template.compile(paperTemp)({
            isShowType:true,//是否显示题目类型
            isShowNum:false,//是否显示题号
            optionList: optionList,//选项  A,B,C...
            quesList: paper,
            type:type
        }));
        $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
        //点击图片查看大图
        $(".timu img").addClass("smallImg");
        $.bigIMG();
        //总题数
        var totalNum=paper.length-1;
        //默认题数
        var defaultNum=0;
        //上一题 下一题
        $(".sectionTest_footer .nextQuestion").click(function(){
            if(!$(this).hasClass("notAvailable")){
                $(".sectionTest_footer .lastQuestion").css("display","inline-block");
                $(".sectionTest_body .timu>li").eq(++defaultNum).css("display","inline-block").siblings().hide();
                $(".curCount").html(defaultNum+1+"/");
                if(!$(".sectionTest_body .timu>li").eq(defaultNum).find(".ExamAnswer li").hasClass("select")){
                    $(".sectionTest_footer .nextQuestion").addClass('notAvailable');
                    //$(".sectionTest_footer .sectionTest_submit").addClass('notAvailable');
                }
                if(defaultNum==totalNum){
                    $(".sectionTest_footer .nextQuestion").hide();
                    if(!$(".sectionTest_body .timu>li").eq(defaultNum).find(".ExamAnswer li").hasClass("select")){
                        $(".sectionTest_footer .sectionTest_submit").css("display","inline-block").addClass('notAvailable');
                    }else{
                        $(".sectionTest_footer .sectionTest_submit").css("display","inline-block").removeClass('notAvailable');
                    }
                }
            }
        });
        $(".sectionTest_footer .lastQuestion").click(function(){
            if(defaultNum<=totalNum){
                $(".sectionTest_footer .sectionTest_submit").hide();
            }
            if(defaultNum<=1){
                $(".sectionTest_footer .lastQuestion").hide();
            }
            $(".sectionTest_body .timu>li").eq(--defaultNum).css("display","inline-block").siblings().hide();
            $(".curCount").html(defaultNum+1+"/");
            $(".sectionTest_footer .nextQuestion").removeClass('notAvailable');
            $(".sectionTest_footer .nextQuestion").css("display","inline-block");
        });
        var answer, answerd = [],answerInfos=[]//回答答案;
        //单选，多选，判断选择效果
        $.effectOfRadio();

        $(".timu li").click(function () {
            var questionId = $(this).parent().parent().attr("data-questionHistoryId");
            var checkPointId = $(this).parent().parent().attr("data-checkPointId");
            if ($(this).hasClass("danxuanTi") || $(this).hasClass("panduanTi")) {
                if ($(this).hasClass("select")) {
                    $(".sectionTest_footer .nextQuestion,.sectionTest_footer .sectionTest_submit").removeClass('notAvailable');
                    answer = $(this).attr("data-selectAnswer");
                } else {
                    $(".sectionTest_footer .nextQuestion,.sectionTest_footer .sectionTest_submit").addClass('notAvailable');
                    answer = "";
                }
                if ($(this).hasClass("panduanTi")) {
                    if ($(this).hasClass("select")) {
                        $(".sectionTest_footer .nextQuestion,.sectionTest_footer .sectionTest_submit").removeClass('notAvailable');
                        if (answer == 0) {
                            answer = "对"
                        } else {
                            answer = "错"
                        }
                    } else {
                        $(".sectionTest_footer .nextQuestion,.sectionTest_footer .sectionTest_submit").addClass('notAvailable');
                        answer = "";
                    }
                }
                answerInfos=$.dealRepeatData({"checkPointId":checkPointId,"questionHistoryId":questionId,"answer":answer});
            } else if ($(this).hasClass("duoxuanTi")) {
                answerd = [];
                var selectlen=$(this).parent().find("li.select").length;
                if($(this).parents(".timu>li").index()==totalNum && selectlen!=0){
                    $(".sectionTest_footer .sectionTest_submit").removeClass('notAvailable');
                }else{
                    $(".sectionTest_footer .sectionTest_submit").addClass('notAvailable');
                }
                if (selectlen!=0) {
                    $(".sectionTest_footer .nextQuestion,.sectionTest_footer .sectionTest_submit").removeClass('notAvailable');
                }else{
                    $(".sectionTest_footer .nextQuestion").addClass('notAvailable');
                }

                $(this).parent().find(".duoxuanTi").each(function () {
                    if ($(this).hasClass("select")) {
                        answerd.push($(this).attr("data-selectanswer"));
                    }
                });
                answerd = JSON.stringify(answerd);
                answerInfos=$.dealRepeatData({"checkPointId":checkPointId,"questionHistoryId":questionId,"answer":answerd});
            }
            if(totalNum==0){
                $(".sectionTest_footer .sectionTest_submit").css('display','inline-block');
            }
        });

        $(".sectionTest_submit").click(function(){
            submitAnswer();
        });
        function submitAnswer() {
            if(!$(".sectionTest_submit").hasClass("notAvailable")){
                var answerInfosSet=JSON.stringify(answerInfos);
                RequestService("/sectionTest/saveSectionTestQuestionResult", "POST", {
                    courseId:courseId,
                    sectionId:sectionId,
                    answerInfos:answerInfosSet
                }, function (data) {
                    if (data.status==200) {
                        resultId=data.result;
                        answerInfos=$.dealRepeatData("");
                        sectionTestResult(data.result,true);
                    }else if(data.status==401){
                        $('#login').modal('show');
                    }
                });
            }
        };

    });
}

//小节测试结果页
//sectionTestResult(32,false)
/**
 *
 * @param id 学员小节测试结果id
 * @param isShowFooter  是否显示 弹框下面的按钮（重新测试  继续学习） true 显示  false 不显示
 * @param fun  传入回调函数
 */
function sectionTestResult(id,isShowFooter,fun){
    RequestService('/sectionTest/findSectionResult',"get",{id: id},function(data){
        var sectionTestInfo=data.result;
        var sectionTestDetailVos=data.result.sectionTestDetailVos;
        $(".scetionTest_lists").html(template.compile(sectionTestResultTemp)({
            items:sectionTestInfo,
            isShowFooter:isShowFooter
        }));
        $(".scetionTest_lists .timu").html(template.compile(paperResultTemp)({
            isShowNum:false,//是否显示题号
            isShowType:true,//是否显示题目类型
            optionList: optionList,//选项  A,B,C...
            canKaoAns:canKaoAns,
            quesList: sectionTestDetailVos,
            type:type
        }));
        $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
        if(data.result.accuracy>60){
            $('.xjcs').each(function(){
                if($(this).hasClass('active')){
                    if($(this).find('jd').length<0){
                        $(this).append('<i class="jd done"></i>')
                    }
                }
            })
        }
        //点击图片查看大图
        $(".timu img").addClass("smallImg");
        $.bigIMG();
        //重新测试
        $(".sectionTest_footer .reTest").click(function(){
            sectionTest(tools.getString('courseId'),tools.getString('moduleId'),sessionStorage.getItem('videoSectionId'));
        });
        //多选题答案结果显示
        $.duoxuanAnswerResult();
        //调用传入函数
        if(fun){
            fun();
        }
    })
}